package com.aaa.service.impl;

import com.aaa.mapper.EchartsMapper;
import com.aaa.service.EchartsService;
import com.aaa.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EchartsServiceImpl implements EchartsService {
    @Autowired
    private EchartsMapper echartsMapper;
    //sql不一定查询前端响应数据的格式---需要在业务层转换为前端希望的格式
    @Override
    public R selectZhuData() {
        List<Map<String,Object>> list = echartsMapper.selectZhuData();
        //{k:[],k2:[]}
        List<String> keys=new ArrayList<>(); //柱状图的名称
        List<Object> values=new ArrayList<>();//柱状图的数量
        for(Map<String,Object> m:list){
            String goodsname = (String) m.get("goodsname");
            Object s =  m.get("s");
            keys.add(goodsname);
            values.add(s);
        }
        //封装到Map
        Map<String,Object> result=new HashMap<>();
        result.put("categories",keys);
        result.put("values",values);
        return new R(200,"获取柱状图数据成功",result);
    }

    @Override
    public R selectPieData() {
        List<Map<String,Object>> list = echartsMapper.selectZhuData();
        //[
        //   {"goodsname":"黑峡谷","s":10},
        //   {"goodsname":"黑神武装","s”：8}
        // ]

        //[
        //   {"value":10,"name":"黑峡谷”},
        //   {"value":8,"name":"黑神武装”}
        // ]
        List<Map<String,Object>> result=new ArrayList<>();
        for(Map<String,Object> m:list){
             Map<String,Object> r=new HashMap<>();
             r.put("name",m.get("goodsname"));
             r.put("value",m.get("s"));
             result.add(r);
        }

        return new R(200,"获取饼图数据成功",result);
    }
}

















